package com.ccc.orm.util;

import org.apache.commons.beanutils.BeanUtils;
import org.apache.commons.beanutils.ConvertUtils;

import java.lang.reflect.InvocationTargetException;
import java.util.Map;

/**
 * 转换工具类
 * 对于{@link org.apache.commons.beanutils.BeanUtils},{@link org.apache.commons.beanutils.ConvertUtils}装饰者模式
 *
 * @author charry
 * @version 0.1
 * @since 2019/6/30 19:11
 */
public class OrmConvertUtils extends BaseUtils {

    private OrmConvertUtils() {
    }

    public static Object convert(Object value, Class<?> targetType) {
        return ConvertUtils.convert(value, targetType);
    }

    public static Object mapConvertToBean(Map<String, Object> map, Class<?> beanClass) throws IllegalAccessException, InstantiationException, InvocationTargetException {

        Object bean = beanClass.newInstance();
        for (String name : map.keySet()) {
            BeanUtils.setProperty(bean, name, map.get(name));
        }

        return bean;
    }
}
